// ============================================================================================================================================
    // ============================================================================================================================================
    //
    //
    // GROUND 地面裁切模拟
    //
    //
    // ============================================================================================================================================
    #ifndef UNITY_SHADER_DT_GROUND_UTIL_INCLUDED  
    #define UNITY_SHADER_DT_GROUND_UTIL_INCLUDED
        // ==================================================================
        // 引用
        // ==================================================================
        //  #include "XXXXXX.hlsl"
        //
        // ==================================================================
        // 使用示例
        // ================================================================== 
            // #if defined(DT_USE_GROUND_S)
            //         GroundResult _groundRes =  CalcGroundResult_S(/*_isOn*/ _GroundIson, /*_InputData*/ i.positionWS.y
            //             ,/*_Factor*/ _GroundFactor,/*_soft*/ _GroundSoft); 
            // #elif defined(DT_USE_GROUND_SW)
            //     GroundResult _groundRes = CalcGroundResult_SW(/*_isOn*/ _GroundIson, /*_InputData*/ i.positionWS.y
            //         ,/*_Factor*/ _GroundFactor,/*_soft*/ _GroundSoft  
            //         ,/*_wide*/ _GroundWide,/*_widesoft*/ _GroundWideSoft, /*EdgeColor*/ _GroundWideColor);
            // #else 
            //     GroundResult _groundRes  =   CalcGroundResult(/*_isOn*/ _GroundIson, /*_InputData*/ i.positionWS.y
            //         ,/*_Factor*/ _GroundFactor );
            // #endif 
        // ==================================================================
        // 结构体
        // ================================================================== 

        // 输出的结果
        struct GroundResult
        {
            float Alpha;//DissolveValue; //溶解值
            float Edge; //溶解边缘
            float3 EdgeColor; //溶解边缘颜色
        }; 
        // ==================================================================
        // 计算  
        // ==================================================================
        GroundResult CalcGroundResult(float _IsOn, float _InputData, float _Factor)
        {
            GroundResult r = (GroundResult)0;
            r.Alpha = 1;
            r.Edge = 0; 
            r.EdgeColor = float3(0,0,0);
            if(_IsOn > 0.5) 
            {
                float clipH = _Factor;                              
                // 计算
                r.Alpha = smoothstep(clipH, clipH, _InputData);                            
            }                  
            return r;
        }
        GroundResult CalcGroundResult_S(float _IsOn, float _InputData, float _Factor,float _Soft)
        {
            GroundResult r = (GroundResult)0;
            r.Alpha = 1;
            r.Edge = 0; 
            r.EdgeColor = float3(0,0,0);
            if(_IsOn > 0.5) 
            {
                float clipH = _Factor;
                float _soft = max(0.0, _Soft);                
                // 计算
                r.Alpha = smoothstep(clipH, clipH + _soft, _InputData);                            
            }                  
            return r;
        }

        GroundResult CalcGroundResult_SW(float _IsOn, float _InputData, float _Factor,
            float _Soft, float _Wide, float _Widesoft,float4 EdgeColor)
        {
            GroundResult r = (GroundResult)0;
            r.Alpha = 1;
            r.Edge = 0; 
            r.EdgeColor = float3(0,0,0);
            if(_IsOn > 0.5) 
            {
                float clipH = _Factor;
                float _soft = max(0.0, _Soft);
                float _wide = _Wide;
                float _widesoft = max(0.0, _Widesoft);
                r.EdgeColor = EdgeColor.rgb * EdgeColor.a;
                // 计算
                r.Alpha = smoothstep(clipH, clipH + _soft, _InputData); 
                r.Edge = 1.0 - smoothstep(clipH + _wide, clipH + _wide + _widesoft, _InputData);                 
            }                  
            return r;
        }

        GroundResult CalcGroundResult_SWD(float _IsOn, float _InputData, float _Factor,  
            float _Soft, float _Wide, float _Widesoft,float4 EdgeColor,DistortResult _DistResult)
        {
            GroundResult r = (GroundResult)0;
            r.Alpha = 1;
            r.Edge = 0; 
            r.EdgeColor = float3(0,0,0);
            if(_IsOn > 0.5) 
            {
                float clipH = _Factor;
                float _soft = max(0.0, _Soft);
                float _wide = _Wide;
                float _widesoft = max(0.0, _Widesoft);                
                r.EdgeColor = EdgeColor.rgb * EdgeColor.a;
                // 计算
                r.Alpha = smoothstep(clipH, clipH + _soft, _InputData); 
                r.Edge = 1.0 - smoothstep(clipH + _wide, clipH + _wide + _widesoft, _InputData); 
 
                float2 tmpv = float2(r.Alpha,r.Edge); 
                AppendDistortResultV1(_DistResult,r.Alpha);
                AppendDistortResultV1(_DistResult,r.Edge);
                tmpv = saturate(tmpv);
                r.Alpha = tmpv.x;
                r.Edge = tmpv.y;
            }                  
            return r;
        }

        GroundResult CalcGroundResult_Base(float _IsOn, float _InputData, float _Factor,  
            float _Soft, float _Wide, float _Widesoft,float4 EdgeColor,DistortResult _DistResult)
        {
            GroundResult r = (GroundResult)0;
            r.Alpha = 1;
            r.Edge = 0; 
            r.EdgeColor = EdgeColor.rgb * EdgeColor.a;
            if(_IsOn > 0.5) 
            {
                float clipH = _Factor;
                float _soft = min(0.0, _Soft);
                float _wide = _Wide;
                float _widesoft = min(0.0, _Widesoft);
                // 计算
                r.Alpha = smoothstep(clipH, clipH + _soft, _InputData); 
                r.Edge = 1.0 - smoothstep(clipH + _wide, clipH + _wide + _widesoft, _InputData); 
 
                float2 tmpv = float2(r.Alpha,r.Edge); 
                AppendDistortResultV2(_DistResult,tmpv);
                tmpv = saturate(tmpv);
                r.Alpha = tmpv.x;
                r.Edge = tmpv.y;
            }                  
            return r;
        }

        //abort
        void ApplyGroundEffect_A(GroundResult result,inout float alpha)
        {
            alpha = alpha * result.Alpha;
        }
        void ApplyGroundEffect_AC_BLend(GroundResult result,inout float3 color,inout float alpha)
        {
            alpha = alpha * result.Alpha;
            color = lerp(color, result.EdgeColor, result.Edge); 
        }
        void ApplyGroundEffect_AC_Add(GroundResult result,inout float3 color,inout float alpha)
        {
            alpha = alpha * result.Alpha; 
            color += result.EdgeColor * result.Edge;
        }
    
    #endif //UNITY_SHADER_DT_GROUND_UTIL_INCLUDED 